package com.firebird.app.kotlin.stock.trend.view

import com.firebird.app.kotlin.stock.trend.R

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewTreeObserver
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.view.animation.Animation
import android.view.animation.TranslateAnimation

/**
 * 自定义带动画效果的，股票详情页面中 五档与明细的空间
 * @author ljk
 * @date   2019年11月3日
 */
class StockWdMxTabView: RelativeLayout , View.OnClickListener{
	private var img_cursor:ImageView?=null
	private var tv_tag1:TextView?=null
    private var tv_tag2:TextView?=null

	private val whiteColor = resources.getColor(R.color.menu_gray)
	private val redColor = resources.getColor(R.color.menu_red)

	private var currentTab = 0

	private var imgWidth:Int=0

    constructor(context: Context?) : super(context, null) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {

		//this.context = context;
		val  typedArray:TypedArray = context.obtainStyledAttributes(attrs, R.styleable.animationTabWidget)
		currentTab = typedArray.getInt(R.styleable.animationTabWidget_current_index, 0)
		typedArray.recycle()

		inflate(context, R.layout.layout_five_gear_port_detail_tabview, this)

		img_cursor = findViewById<View>(R.id.tag_img_cursor) as ImageView

		tv_tag1 = findViewById<View>(R.id.tv_tag1) as TextView
		tv_tag2 = findViewById<View>(R.id.tv_tag2) as TextView

		tv_tag1!!.setOnClickListener(this)
		tv_tag2!!.setOnClickListener(this)
		//获取视图的尺寸
		var vto:ViewTreeObserver = viewTreeObserver
		vto.addOnGlobalLayoutListener( OnGlobalLayoutListener() {

			 fun onGlobalLayout() {
				//监听器注册后每次视图尺寸发生变化时都会调用，如果只想获取一次视图的尺寸，则需要在获取到视图尺寸后注销掉该监听器
				viewTreeObserver.removeGlobalOnLayoutListener(){}
				// this.getViewTreeObserver().removeGlobalOnLayoutListener(this)
				initDefaultIndex(width,height)
			}
		})
	}

	/**
	 * 初始化 默认 指示位置
	 * @param index 从1开始
	 */
	private fun initDefaultIndex( screenW:Int, screenH:Int){

		imgWidth = screenW/2

		//var para:LayoutParams = img_cursor.layoutParams
		val para = img_cursor!!.layoutParams
		para.width = imgWidth
		img_cursor!!.layoutParams=para

		setCurrentTab(currentTab)
	}

	override fun onClick(v:View) {

		when (v.id) {
			 R.id.tv_tag1->
				currentTab = 0
			 R.id.tv_tag2->
				currentTab = 1
		}

		setCurrentTab(currentTab)
	}

	/**
	 * 设置当前指示器 位置
	 * @param position
	 */
	fun setCurrentTab(position:Int){

		tv_tag1!!.textSize=12f
		tv_tag2!!.textSize=12f

		tv_tag1!!.setTextColor(whiteColor)
		tv_tag2!!.setTextColor(whiteColor)

        when(position) {
			 0->{
				tv_tag1!!.setTextColor(redColor)
				tv_tag1!!.textSize=16f
			 }
			 1->{
				tv_tag2!!.setTextColor(redColor)
				tv_tag2!!.textSize=16f
			 }
		}

		var animation:Animation ?= null
		animation = TranslateAnimation(imgWidth*currentTab.toFloat(), imgWidth*position.toFloat(), 0f, 0f)
		animation.setFillAfter(true)//True:图片停在动画结束位置
		animation.setDuration(300)	//设置动画完成时间
		img_cursor!!.startAnimation(animation);

		currentTab = position
	}

}